package com.lxc.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxc.entity.Authors;
import com.lxc.entity.Result;
import com.lxc.service.AuthorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/authors")
@Slf4j
@CrossOrigin
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    // 查询所有作者
    @GetMapping
    public Result<List<Authors>> getAllAuthors(@RequestParam(defaultValue = "1",  required = false) Integer currentPage,
                                        @RequestParam(defaultValue = "5",  required = false) Integer pageSize){
        return Result.success(authorService.getAllAuthors(currentPage,pageSize));
    }

    // 查询作者详细信息
    @GetMapping("/{id}")
    public Result<Authors> getAuthorDetail(@PathVariable Integer id){
        return Result.success(authorService.getAuthorDetail(id));
    }

    // 新增方法：添加作者信息
    @PostMapping
    public Result addAuthor(@RequestPart("author") Authors author,
                            @RequestParam("file") MultipartFile file) throws IOException {
        return Result.success(authorService.saveAuthor(author,file));
    }
}